package org.jackysoft.entity;

import java.util.Arrays;

import javax.persistence.Column;
import javax.persistence.Lob;
import javax.persistence.MappedSuperclass;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Type;
import org.jackysoft.util.AttachmentType;
import org.joda.time.DateTime;

@MappedSuperclass
public abstract class AbstractAppendinceItem extends AbstractEntity implements AppendinceItem,Cloneable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7951370107078052074L;

	protected byte[] data = new byte[0];
	protected String name;
	protected AttachmentType type;
	protected String path;
	protected String ext;
	protected DateTime createDate;
 	
	
	@Lob
	@Column(length = 256 * 65536)
	@JSON(serialize=false)
	public byte[] getData() {
		return data;
	}

	/**
	 * @param content
	 *            要设置的 content
	 */
	public void setData(byte[] content) {
		this.data = content;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public AttachmentType getType() {
		return type;
	}

	public void setType(AttachmentType type) {
		this.type = type;
	}

	public String getExt() {
		
		return this.ext;
	}

	public void setExt(String ext) {
		this.ext = ext;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	/*
	 * （非 Javadoc）
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + Arrays.hashCode(data);
		result = PRIME * result
				+ ((path == null) ? 0 : path.hashCode());
		result = PRIME * result + ((id == null) ? 0 : id.hashCode());
		result = PRIME * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/*
	 * （非 Javadoc）
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final AbstractAppendinceItem other = (AbstractAppendinceItem) obj;
		if (!Arrays.equals(data, other.data))
			return false;
		if (path == null) {
			if (other.path != null)
				return false;
		} else if (!path.equals(other.path))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Type(type = "jodatime")
	public DateTime getCreateDate() {
		return createDate;
	}

	public void setCreateDate(DateTime createDate) {
		this.createDate = createDate;
	}
	
}
